package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.entity.realTime.EntityAddCashUserByHour;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewDetailData;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 实时数据-小时付费帐号数manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class AddCashUserByHourManager extends ManagerQueryCacheBase<EntityAddCashUserByHour> {
	private static final long serialVersionUID = 1L;
	
	public List<EntityAddCashUserByHour> getChannelHourPayUserByDate(Date date, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityAddCashUserByHour where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid !=:channelid and serverid=:serverid order by hour desc ", "dayStart", new Date(Time.getBeginningOfDay(date.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(date.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}
	
	public List<EntityAddCashUserByHour> getHourPayUserByDate(Date date, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityAddCashUserByHour where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid order by hour ", "dayStart", new Date(Time.getBeginningOfDay(date.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(date.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}
	
	// 根据平台、渠道、区服更新小时付费帐号数数据
	public void updateHourPayUserByConditions(String updateType, Map<Integer, OverviewDetailData> map, Date lookDate, Date compareDate, Date lastWeekDate, String queryPlatform, String queryChannel, String queryServer) {
		List<EntityAddCashUserByHour> ooLookDate = getHourPayUserByDate(lookDate, queryPlatform, queryChannel, queryServer);
		List<EntityAddCashUserByHour> ooCompareDate = getHourPayUserByDate(compareDate, queryPlatform, queryChannel, queryServer);
        List<EntityAddCashUserByHour> ooLastWeekDate = getHourPayUserByDate(lastWeekDate, queryPlatform, queryChannel, queryServer);
        if ("1".equals(updateType)) {  // 分时
            updateData(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        } else if ("2".equals(updateType)) {  // 累加
            updateData1(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        }
	}
	
	// 更新结果-分时
	private void updateData(Map<Integer, OverviewDetailData> map, List<EntityAddCashUserByHour> ooLookDate, List<EntityAddCashUserByHour> ooCompareDate, List<EntityAddCashUserByHour> ooLastWeekDate) {
		for (EntityAddCashUserByHour o : ooLookDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setPayUserLookValue(o.getAddcash_user() + odd.getPayUserLookValue());
			}
		}
		for (EntityAddCashUserByHour o : ooCompareDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setPayUserCompareValue(o.getAddcash_user() + odd.getPayUserCompareValue());
			}
		}
        for (EntityAddCashUserByHour o : ooLastWeekDate) {
            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                odd.setPayUserLastWeekValue(o.getAddcash_user() + odd.getPayUserLastWeekValue());
            }
        }
	}

    // 更新结果-累计
    private void updateData1(Map<Integer, OverviewDetailData> map, List<EntityAddCashUserByHour> ooLookDate, List<EntityAddCashUserByHour> ooCompareDate, List<EntityAddCashUserByHour> ooLastWeekDate) {
        int nowAdd1 = 0;
        int index1 = 0;
        for (EntityAddCashUserByHour o : ooLookDate) {
            if(o.getHour() > index1++) {
                for (int i = index1 - 1; i < o.getHour(); i++) {
                    OverviewDetailData odd = map.get(i);
                    if (odd != null) {
                        odd.setPayUserLookValue(nowAdd1);
                    }
                }
            }

            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                nowAdd1 += o.getAddcash_user() + odd.getPayUserLookValue();
                odd.setPayUserLookValue(nowAdd1);
            }
        }

        int nowAdd2 = 0;
        int index2 = 0;
        for (EntityAddCashUserByHour o : ooCompareDate) {
            if(o.getHour() > index2++) {
                for (int i = index2 - 1; i < o.getHour(); i++) {
                    OverviewDetailData odd = map.get(i);
                    if (odd != null) {
                        odd.setPayUserCompareValue(nowAdd2);
                    }
                }
            }

            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                nowAdd2 += o.getAddcash_user() + odd.getPayUserCompareValue();
                odd.setPayUserCompareValue(nowAdd2);
            }
        }

        int nowAdd3 = 0;
        int index3 = 0;
        for (EntityAddCashUserByHour o : ooLastWeekDate) {
            if(o.getHour() > index3++) {
                for (int i = index3 - 1; i < o.getHour(); i++) {
                    OverviewDetailData odd = map.get(i);
                    if (odd != null) {
                        odd.setPayUserLastWeekValue(nowAdd3);
                    }
                }
            }

            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                nowAdd3 += o.getAddcash_user() + odd.getPayUserLastWeekValue();
                odd.setPayUserLastWeekValue(nowAdd3);
            }
        }
    }
}
